import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;


public class RtpSession {

	public static final int MAX_PACKET_SIZE = 524; //12 Bytes Header + 512 Bytes Payload
	private InetAddress myIpAddress = null;
	private String remoteIpAdress = null;
	private InetAddress remoteAdress = null;
	private DatagramSocket mySendSocket = null;
	private DatagramSocket myRecvSocket = null;
	private int myPort = 0;
	private int remotePort = 0;
	private int sequenceNumber = 0;
	
	public RtpSession(byte[] rtpPacketByte, int length) {
		
		
	}
	
	public RtpSession(int myPort) {		
		
		this.myPort = myPort;
		
		try {
			this.myIpAddress = InetAddress.getLocalHost();
			this.myRecvSocket = new DatagramSocket(myPort);
			this.mySendSocket = this.myRecvSocket; 
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}	
	
	public void setRemoteIpAdress(String remoteIpAdress) {
		
		this.remoteIpAdress = remoteIpAdress;
		//try {
			//this.remoteAdress = InetAddress.getByName(remoteIpAdress);
		//} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
		//	e.printStackTrace();
		//}
	}
	
	public void setRemotePort(int remotePort) {
		
		this.remotePort = remotePort;
	}
	
	public void send(RtpPacket rtpPacket) {
		
		byte[] rtpPacketBytes = rtpPacket.getRtpPacket();		
		DatagramPacket datagramPacket = new DatagramPacket(new byte[1], 1, remoteAdress, remotePort);
		
		datagramPacket.setData(rtpPacketBytes);
		try {
			this.mySendSocket.send(datagramPacket);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	RtpPacket recv() {		
		
		byte[] buffer = new byte[MAX_PACKET_SIZE];
		DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
		//RtpPacket rtpPacket = new RtpPacket(packet.getData(), packet.getData().length);
		RtpPacket rtpPacket;
		try {
			
			System.out.println("Server is running with IP: "+this.myIpAddress.getHostAddress().toString());
			
			this.myRecvSocket.receive(packet);
			
			this.setRemoteIpAdress(packet.getAddress().toString());
			this.setRemotePort(packet.getPort());
			
			byte[] rptPacketBytes = packet.getData();
			int rtpPacketLength = packet.getLength();
			
			System.out.println("Getting Packet");
			
			rtpPacket = new RtpPacket(rptPacketBytes, rtpPacketLength);			
			rtpPacket.printRtpPacket();
			
			
			return rtpPacket;
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null ;
	}
	
	public void closeSession() {
		
	}
	
	
	public InetAddress getMyIpAdress() {
		
		return this.myIpAddress;
	}
	
	public int getMyPort() {
		
		return this.myPort;
	}
	
	public InetAddress getRemoteIpAddress() {
		
		try {
			return InetAddress.getByName(this.remoteIpAdress);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block			
			e.printStackTrace();
		}
		
		return null;
	}
	
	public int getRemotePort() {
		
		return this.remotePort;
	}
	
	
	
}
